<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入客户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="助记码" prop="easyCode">
        <el-input
          v-model="queryParams.easyCode"
          placeholder="请输入助记码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户类型" prop="customerCategory">
        <el-select v-model="queryParams.customerCategory" placeholder="请输入客户类型">
          <el-option
            v-for="item in customerCtgDropdownList"
            :key="item.code"
            :label="item.name"
            :value="item.code">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="区域" prop="area">
        <el-select v-model="queryParams.area" placeholder="请选择区域" clearable>
          <el-option
            v-for="dict in dict.type.mes_area"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basic:customer:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basic:customer:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basic:customer:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basic:customer:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed="left"/>
      <el-table-column label="客户名称" align="center" prop="name" min-width="100px" sortable/>
      <el-table-column label="客户编号" align="center" prop="code" min-width="100px" sortable/>
      <el-table-column label="助记码" align="center" prop="easyCode" min-width="100px" sortable/>
      <el-table-column label="客户类型" align="center" prop="customerCategory" min-width="100px" sortable/>
      <el-table-column label="联系人" align="center" prop="contact" min-width="100px" sortable/>
      <el-table-column label="电话" align="center" prop="phone" sortable/>
      <el-table-column label="手机" align="center" prop="mobile" sortable/>
      <el-table-column label="地址" align="center" prop="address" sortable/>
      <el-table-column label="区域" align="center" prop="area" sortable>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_area" :value="scope.row.area"/>
        </template>
      </el-table-column>
      <el-table-column label="E-mail" align="center" prop="email" min-width="100px" sortable/>
      <el-table-column label="网址" align="center" prop="website" sortable/>
      <el-table-column label="传真" align="center" prop="fax" sortable/>
      <el-table-column label="开户行" align="center" prop="openingBank" min-width="100px" sortable/>
      <el-table-column label="开户名" align="center" prop="accountName" min-width="100px" sortable/>
      <el-table-column label="税号" align="center" prop="taxIdNumber" sortable/>
      <el-table-column label="银行账号" align="center" prop="bankAccount" min-width="100px" sortable/>
      <el-table-column label="审核状态" align="center" prop="auditStatus" min-width="100px" sortable/>
      <el-table-column label="备注" align="center" prop="remark" sortable/>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" min-width="120px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:customer:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basic:customer:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户对话框 -->
     <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="85px">
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="客户名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入客户名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户编号" prop="code">
              <el-input v-model="form.code" placeholder="请输入客户编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="助记码" prop="easyCode">
              <el-input v-model="form.easyCode" placeholder="请输入助记码"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="客户类型" prop="customerCategory">
              <el-select v-model="form.customerCategory" placeholder="请输入客户类型">
                <el-option
                  v-for="item in customerCtgDropdownList"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人" prop="contact">
              <el-input v-model="form.contact" placeholder="请输入联系人"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入电话"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="手机" prop="mobile">
              <el-input v-model="form.mobile" placeholder="请输入手机"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="区域" prop="area">
              <el-select v-model="form.area" placeholder="请选择区域">
                <el-option
                  v-for="dict in dict.type.mes_area"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="E-mail" prop="email">
              <el-input v-model="form.email" placeholder="请输入E-mail"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="网址" prop="website">
              <el-input v-model="form.website" placeholder="请输入网址"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入地址"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="传真" prop="fax">
              <el-input v-model="form.fax" placeholder="请输入传真"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="开户行" prop="openingBank">
              <el-input v-model="form.openingBank" placeholder="请输入开户行"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="开户名" prop="accountName">
              <el-input v-model="form.accountName" placeholder="请输入开户名"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="税号" prop="taxIdNumber">
              <el-input v-model="form.taxIdNumber" placeholder="请输入税号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="银行账号" prop="bankAccount">
              <el-input v-model="form.bankAccount" placeholder="请输入银行账号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listCustomer, getCustomer, delCustomer, addCustomer, updateCustomer} from "@/api/basic/customer";
import {getCustomerCtgDropdownList} from "../../../api/basic/customerctg";

export default {
  name: "Customer",
  dicts: ['mes_area'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        code: null,
        easyCode: null,
        customerCategory: null,
        contact: null,
        phone: null,
        mobile: null,
        address: null,
        area: null,
        email: null,
        website: null,
        fax: null,
        openingBank: null,
        accountName: null,
        taxIdNumber: null,
        bankAccount: null,
        auditStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "客户名称不能为空", trigger: "blur"}
        ], code: [
          {required: true, message: "客户编号不能为空", trigger: "blur"}
        ],
      },
      customerCtgDropdownList:[]
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.getCustomerCtgList()
  },
  methods: {
    getCustomerCtgList(){
      getCustomerCtgDropdownList().then(res=>{
        this.customerCtgDropdownList = res.data
      })
    },
    /** 查询客户列表 */
    getList() {
      this.loading = true;
      listCustomer(this.queryParams).then(response => {
        this.customerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        code: null,
        easyCode: null,
        customerCategory: null,
        contact: null,
        phone: null,
        mobile: null,
        address: null,
        area: null,
        email: null,
        website: null,
        fax: null,
        openingBank: null,
        accountName: null,
        taxIdNumber: null,
        bankAccount: null,
        auditStatus: "0",
        delFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCustomer(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCustomer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户编号为"' + ids + '"的数据项？').then(function () {
        return delCustomer(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basic/customer/export', {
        ...this.queryParams
      }, `customer_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
